package com.qbw.app.module.user.fragment;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.http.okhttp.HttpTask;
import com.lib.util.NetUtil;
import com.lib.util.ResUtil;
import com.lib.util.Util;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.databinding.UserFragmentRegistPhoneBinding;
import com.qbw.app.model.Country;
import com.qbw.app.module.common.activity.SelectCountryAreaActivity;
import com.qbw.app.module.common.fragment.CommonFragment;
import com.qbw.app.module.common.util.HttpRequestUtil;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.module.user.activity.UserRegistActivity;
import com.qbw.app.request.impl.CommonRequest;
import com.qbw.core.observable.ObservableString;

import java.util.regex.Pattern;

import static com.qbw.app.module.user.activity.UserRegistActivity.FRAGMENT_VERIFYCODE;
import static com.qbw.app.module.user.activity.UserRegistActivity.REQUEST_CODE_SELECT_COUNTRY_AREA;

/**
 * @author QBW
 * @date 2019/9/16
 */
public class UserRegistPhoneFragment extends CommonFragment<UserFragmentRegistPhoneBinding> {

    private ObservableString mError = new ObservableString();

    public static UserRegistPhoneFragment createInstance() {
        UserRegistPhoneFragment ins = new UserRegistPhoneFragment();
        return ins;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.user_fragment_regist_phone;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        UserRegistActivity activity = (UserRegistActivity) getActivity();
        mBinding.setCountryArea(activity.getCountryArea());
        mBinding.setPhone(activity.getPhone());
        mBinding.setError(mError);
        mBinding.executePendingBindings();
        mBinding.tvCountryAreaValue.setOnClickListener(this);
        mBinding.tvNextStep.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_next_step) {
            Country selectedCountry = getSelectedCountry();
            if (TextUtils.isEmpty(mBinding.getCountryArea()
                                          .getValue()) || selectedCountry == null) {
                mError.setValue(ResUtil.getString(R.string.please_select_country_area));
                return;
            }
            /*if (TextUtils.isEmpty(mBinding.getPhone().getValue())) {
                UniversalToastUtil.showToast(R.string.input_phonenumber);
                return;
            }*/
            if (!Pattern.compile(selectedCountry.getPhoneJs())
                        .matcher(mBinding.getPhone().getValue())
                        .matches()) {
                mError.setValue(ResUtil.getString(R.string.invalid_phone_format));
                return;
            }
            /*showLoadingDialog("");
            mGeneralManager.getRequestManager()
                           .getCommonRequest()
                           .checkUserExist(mBinding.getCountryArea().getValue(),
                                           mBinding.getPhone().getValue(),
                                           this)
                           .execute();*/
            pendingGetVerifyCode();
        } else if (vid == R.id.tv_country_area_value) {
            SelectCountryAreaActivity.startForResult(getActivity(),
                                                     REQUEST_CODE_SELECT_COUNTRY_AREA);
        }
    }

    private void pendingGetVerifyCode() {
        Util.toogleKeybord(getActivity(), false);
        showLoadingDialog("");
        mGeneralManager.getRequestManager()
                       .getCommonRequest()
                       .getVerifyCode(mBinding.getCountryArea().getValue(),
                                      mBinding.getPhone().getValue(),
                                      Constant.VERIFYCODE_TYPE_REGIST,
                                      this)
                       .execute();
    }

    private Country getSelectedCountry() {
        UserRegistActivity activity = (UserRegistActivity) getActivity();
        return activity.getSelectedCountry();
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(CommonRequest.METHOD_VERIFY_CODE)) {
            closeLoadingDialog();
            UserRegistActivity activity = (UserRegistActivity) getActivity();
            activity.switchFragment(FRAGMENT_VERIFYCODE);
        } else if (method.equals(CommonRequest.METHOD_USER_EXIST)) {
            pendingGetVerifyCode();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        message = HttpRequestUtil.getMessage(errorCode, message);
        String method = httpTask.getMethod();
        if (method.equals(CommonRequest.METHOD_VERIFY_CODE)) {
            closeLoadingDialog();
            if(NetUtil.isNetAvailable(getActivity())) {
                mError.setValue(message);
            } else {
                UniversalToastUtil.showFailedIconToast(message);
            }
        } else if (method.equals(CommonRequest.METHOD_USER_EXIST)) {
            UniversalToastUtil.showFailedIconToast(message);
            closeLoadingDialog();
        }
    }
}
